//
//  SettingViewController.m
//  HWZB
//
//  Created by 许成雄 on 2017/4/23.
//  Copyright © 2017年 wonders. All rights reserved.
//

#import "SettingViewController.h"
#import "SettingTableViewCell.h"
#import "ModifyPasswordViewController.h"
#import "AppDelegate.h"
#import "Context.h"

@interface SettingViewController () <UITableViewDelegate, UITableViewDataSource, UIAlertViewDelegate>

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *itemArray;
@property (strong, nonatomic) UIButton *logoutButton;

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = I_COLOR_BACKGROUND;
    [self setBackNavgationItem];
    self.navigationItem.title = @"设置";
    
    [self createUI];
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - UIButton Action
- (void)logoutButtonAction:(id)sender {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您确定要退出登录?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(buttonIndex == 1) {
        [Context sharedInstance].loginID = nil;
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationUserLogin object:nil];
        [self.navigationController popViewControllerAnimated:NO];
        [[AppDelegate sharedDelegate] showMainViewController];
    }
}


#pragma mark - UITableViewDelegate && UITableViewDatasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger count = self.itemArray != nil ? self.itemArray.count : 0;
    return count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return TRANS_VALUE(44.0f);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"SettingTableViewCell";
    SettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if(!cell) {
        cell = [[SettingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if(indexPath.row < self.itemArray.count) {
        SettingItemInfo *settingItem = [self.itemArray objectAtIndex:indexPath.row];
        cell.settingItem = settingItem;
        if(indexPath.row == 1) {
            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            // app版本
            NSString *versionStr = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
            [cell showAppVersion:versionStr];
            cell.accessoryType = UITableViewCellAccessoryNone;
        } else {
            [cell showAppVersion:nil];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == 0) {
        ModifyPasswordViewController *modifyPasswordViewController = [[ModifyPasswordViewController alloc] init];
        [self.navigationController pushViewController:modifyPasswordViewController animated:YES];
    }
    
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row < self.itemArray.count) {
        if([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsZero];
        }
        if([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
    } else {
        if([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsZero];
        }
        if([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
    }
}


#pragma mark - Private Method
- (void)createUI {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64.0f) style:UITableViewStylePlain];
    self.tableView.backgroundColor = I_COLOR_WHITE;
    self.tableView.separatorColor = I_COLOR_DIVIDER;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.scrollEnabled = NO;
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[SettingTableViewCell class] forCellReuseIdentifier:@"SettingTableViewCell"];
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, TRANS_VALUE(0.0f))];
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(132.0f), SCREEN_WIDTH, SCREEN_HEIGHT - TRANS_VALUE(88.0f) - 64.0f)];
    footerView.backgroundColor = I_COLOR_WHITE;
    self.tableView.tableFooterView = footerView;
    UIView *dividerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5f)];
    dividerView.backgroundColor = I_COLOR_DIVIDER;
    [footerView addSubview:dividerView];
    
    self.logoutButton = [[UIButton alloc] initWithFrame:CGRectMake(TRANS_VALUE(25.0f), TRANS_VALUE(20.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(25.0f), TRANS_VALUE(36.0f))];
    self.logoutButton.clipsToBounds = YES;
    self.logoutButton.layer.borderColor = I_COLOR_RED.CGColor;
    self.logoutButton.layer.borderWidth = 1.0f;
    self.logoutButton.titleLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(14.0f)];
    [self.logoutButton setTitleColor:I_COLOR_RED forState:UIControlStateNormal];
    [self.logoutButton setTitleColor:I_COLOR_RED forState:UIControlStateSelected];
    [self.logoutButton setTitle:@"退出登录" forState:UIControlStateNormal];
    [self.logoutButton setTitle:@"退出登录" forState:UIControlStateSelected];
    [footerView addSubview:self.logoutButton];
    
    [self.logoutButton addTarget:self action:@selector(logoutButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //设置tableview分割线
    if([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
}

- (void)loadData {
    if(!self.itemArray) {
        self.itemArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    [self.itemArray removeAllObjects];
    SettingItemInfo *settingItem0 = [[SettingItemInfo alloc] init];
    settingItem0.icon = @"ic_setting_modify_password";
    settingItem0.title = @"修改密码";
    [self.itemArray addObject:settingItem0];
    SettingItemInfo *settingItem1 = [[SettingItemInfo alloc] init];
    settingItem1.icon = @"ic_setting_contact_us";
    settingItem1.title = @"当前版本";
    [self.itemArray addObject:settingItem1];
//    SettingItemInfo *settingItem2 = [[SettingItemInfo alloc] init];
//    settingItem2.icon = @"ic_setting_contact_us";
//    settingItem2.title = @"联系我们";
//    [self.itemArray addObject:settingItem2];
    
    [self.tableView reloadData];
}

@end
